package com.training1.core.web.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.training1.core.entity.DelayMoneyEntity;
import com.training1.core.modules.service.DelayMoneyService;


@Controller
@RequestMapping("delayMoney")
public class DelayMoneyController {
	@Resource
	private  DelayMoneyService delayMoneyService;
	
	//时间工具方法
		public static String formatDate(Date date) throws Exception{
			 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			 return formatter.format(date);
		}
	
		
	@RequestMapping(value={"/",""})
	public String go(String ContractbaseId,String customerName,String phone,Model model) throws Exception{
		if(ContractbaseId==""||ContractbaseId==null||customerName==""||customerName==null||phone==""||phone==null){
			return "role/delayMoney";
		}
		DelayMoneyEntity delay= delayMoneyService.findDelay(ContractbaseId);
		delay.setPhone(phone);
		delay.setCustomerName(customerName);
		delay.setDelyaMoneyList(delayMoneyService.findlist(ContractbaseId));
		BigDecimal sumAmount = new BigDecimal("0");
		for(int i=0;i<delay.getDelyaMoneyList().size();i++){
			sumAmount=sumAmount.add(delay.getDelyaMoneyList().get(i).getDelayInterest()) ;
			delay.getDelyaMoneyList().get(i).setExpRepayDateStr(DelayMoneyController.formatDate(delay.getDelyaMoneyList().get(i).getExpRepayDate()));
		}
		delay.setSumAmount(sumAmount);
		delay.setAceAmount(sumAmount.subtract(delay.getDelayAmount()));
		model.addAttribute("delay", delay);
		String secInsDate=DelayMoneyController.formatDate(delay.getSecInsDate());
		model.addAttribute("secInsDate", secInsDate);
		model.addAttribute("ContractbaseId", ContractbaseId);
		return "role/delayMoney";
		
	}

	/**
	 * 保存延迟服务费
	 */
	@RequestMapping(value ="save",method=RequestMethod.GET,produces = "text/plain")
	@ResponseBody
	public String updateDelayAmount(String ContractbaseId,BigDecimal delayAmount){
		delayMoneyService.updateDelayAmount(ContractbaseId, delayAmount);
		return "1";
	}
}
